/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.schedule.core.config;

import lombok.extern.slf4j.Slf4j;
import org.noear.solon.Solon;
import org.noear.solon.annotation.*;
import org.noear.solon.core.BeanWrap;
import org.noear.solon.scheduling.ScheduledAnno;
import org.noear.solon.scheduling.scheduled.JobHandler;
import org.noear.solon.scheduling.scheduled.wrap.JobBeanWrap;
import org.noear.solon.scheduling.simple.JobManager;

/**
 * <b>(ScheduleConfiguration)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/12/27
 */
@Slf4j
@Component
@Configuration
@Import(scanPackages = {"info.peigen.hotpot.component.mail", "info.peigen.hotpot.common",
        "com.gitee.peigenlpy.actable", "org.apache.ibatis"})
public class ScheduleConfiguration {
    @Inject
    ScheduleProperties scheduleProperties;

    @Bean
    public void job() throws Throwable {
        JobManager jobManager = JobManager.getInstance();

        String        name = ScheduleNodeExecutor.class.getSimpleName();
        ScheduledAnno anno = new ScheduledAnno();
        anno.name("scheduleNone").cron("0/3 * * * * ? *").zone("+00");
        BeanWrap   bw      = Solon.context().beanMake(ScheduleNodeExecutor.class);
        JobHandler handler = new JobBeanWrap(bw);

        jobManager.jobAdd(name, anno, handler);
        if (scheduleProperties.isEnabled()) {
            jobManager.start();
        }

    }
}